<?php
App::uses('AppController', 'Controller');
/**
 * Employees Controller
 *
 * @property Employee $Employee
 */
class EmployeesController extends AppController {

/**
 * index method
 *
 * @return void
 */
	public function index() {
	
	if (CakeSession::read("Auth.User.UserType") != 'Administrator') {
        // No match, redirect the user back to the index action.
        $this->Session->setFlash(__('You cant access this!'));
        $this->redirect('/');
    }
	
		$this->Employee->recursive = 2;
		$this->set('employees', $this->paginate());
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
	
	if (CakeSession::read("Auth.User.UserType") != 'Administrator') {
        // No match, redirect the user back to the index action.
        $this->Session->setFlash(__('You cant access this!'));
        $this->redirect('/');
    }
		if (!$this->Employee->exists($id)) {
			throw new NotFoundException(__('Invalid employee'));
		}
		$options = array('conditions' => array('Employee.' . $this->Employee->primaryKey => $id), 'recursive' => 2);
		
		$this->set('employee', $this->Employee->find('first', $options));
		
	}

/**
 * add method
 *
 * @return void
 */
	public function add() {
	
	if (CakeSession::read("Auth.User.UserType") != 'Administrator') {
        // No match, redirect the user back to the index action.
        $this->Session->setFlash(__('You cant access this!'));
        $this->redirect('/');
    }
		if ($this->request->is('post')) {
			$this->Employee->create();
			
			$this->request->data['EmployeeJobLvl'] = array( '0' => array( 
																	'emp_joblvl_start' => date("Y-m-d"),
																	'emp_joblvl_employee' => $id,
																	'emp_joblvl_code' => $this->request->data['Employee']['EmployeeJobLvl']));
					
					$this->request->data['EmployeeJobType'] = array( '0' => array( 
																	'emp_jobtype_start' => date("Y-m-d"),
																	'emp_jobtype_employee' => $id,
																	'emp_jobtype_code' => $this->request->data['Employee']['EmployeeJobType']));
					$this->request->data['EmployeeJobStatus'] = array( '0' => array( 
																	'emp_jobstatus_start' => date("Y-m-d"),
																	'emp_jobstatus_employee' => $id,
																	'emp_jobstatus_code' => $this->request->data['Employee']['EmployeeJobStatus']));
																	
			if ($this->Employee->saveAll($this->request->data)) {
				$this->Session->setFlash(__('The employee has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The employee could not be saved. Please, try again.'));
			}
			
			
			 $fileOK = $this->uploadFiles('files/cv', $this->request->data['Employee']['e_cv']);
            if(array_key_exists('urls', $fileOK))
            {
                $this->request->data['Employee']['e_cv'] = $fileOK['urls'][0];
				
				if ($this->Employee->saveAll($this->request->data)) 
				{
				$this->Session->setFlash(__('The employee has been saved'));
				$this->redirect(array('action' => 'index'));
				}
				else
				{
					$this->Session->setFlash(__('The employee could not be saved. Please, try again.'));
				}
            }
            else
            {
               $this->request->data['Employee']['e_cv'] = $currentFile;
			   if($this->Employee->saveAll($this->request->data))
			   {
				$this->Session->setFlash(__('The employee has been saved'));
				$this->redirect(array('action' => 'index'));
			   }
			   else
			   {
					$this->Session->setFlash(__('The employee could not be saved. Please, try again.'));
			   }
			}
			
		}
		$this->loadModel('JobLvl');
			$this->loadModel('JobType');
			$this->loadModel('JobStatus');
			$jobLvls = $this->JobLvl->find('list');
			$jobStatuses = $this->JobStatus->find('list');
			$jobTypes = $this->JobType->find('list');
			$this->set(compact('jobLvls', 'jobStatuses', 'jobTypes'));
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
	
	if (CakeSession::read("Auth.User.UserType") != 'Administrator') {
        // No match, redirect the user back to the index action.
        $this->Session->setFlash(__('You cant access this!'));
        $this->redirect('/');
    }
		if (!$this->Employee->exists($id)) {
			throw new NotFoundException(__('Invalid employee'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {

			
			$options = array('conditions' => array("Employee.e_id" => $id));
			$results = $this->Employee->find('first', $options); 
			
			
			
			//save employeejoblvl if different
			$employeeJobLvl = $this->Employee->EmployeeJobLvl->find('first', array('conditions' => array('EmployeeJobLvl.emp_joblvl_employee' => $id), 'order' => 'emp_joblvl_start DESC'));
			if($this->request->data['Employee']['EmployeeJobLvl'] != $employeeJobLvl['EmployeeJobLvl']['emp_joblvl_code'])
			{

				$this->request->data['EmployeeJobLvl'] = array( '0' => array( 
																'emp_joblvl_start' => date("Y-m-d"),
																'emp_joblvl_employee' => $id,
																'emp_joblvl_code' => $this->request->data['Employee']['EmployeeJobLvl']));
			}
			//save employeejobtype if differnt
			//$employeeJobType = $this->Employee->EmployeeJobType->find('first', array('conditions' => array('EmployeeJobType.emp_jobtype_employee' => $id), 'order' => 'emp_jobtype_start DESC'));
			if($this->request->data['Employee']['EmployeeJobType'] != $employeeJobType['EmployeeJobType']['emp_jobtype_code'])
			{
				
				$this->request->data['EmployeeJobType'] = array( '0' => array( 
																'emp_jobtype_start' => date("Y-m-d"),
																'emp_jobtype_employee' => $id,
																'emp_jobtype_code' => $this->request->data['Employee']['EmployeeJobType']));
			}			
			
			$employeeJobStatus = $this->Employee->EmployeeJobStatus->find('first', array('conditions' => array('EmployeeJobStatus.emp_jobstatus_employee' => $id), 'order' => 'emp_jobstatus_start DESC'));
			if($this->request->data['Employee']['EmployeeJobStatus'] != $employeeJobStatus['EmployeeJobStatus']['emp_jobstatus_code'])
			{
				$this->request->data['EmployeeJobStatus'] = array( '0' => array( 
																'emp_jobstatus_start' => date("Y-m-d"),
																'emp_jobstatus_employee' => $id,
																'emp_jobstatus_code' => $this->request->data['Employee']['EmployeeJobStatus']));
			}			
			
			
			
			$currentFile = $results['Employee']['e_cv'];
			$files = $this->request->data['Employee']['e_cv'];

            //upload file
            $fileOK = $this->uploadFiles('files/cv', $this->request->data['Employee']['e_cv']);
            if(array_key_exists('urls', $fileOK))
            {
                $this->request->data['Employee']['e_cv'] = $fileOK['urls'][0];
				
				if ($this->Employee->saveAll($this->request->data)) 
				{
				$this->Session->setFlash(__('The employee has been saved'));
				$this->redirect(array('action' => 'index'));
				}
				else
				{
					$this->Session->setFlash(__('The employee could not be saved. Please, try again.'));
				}
            }
            else
            {
               $this->request->data['Employee']['e_cv'] = $currentFile;
			   if($this->Employee->saveAll($this->request->data))
			   {
				$this->Session->setFlash(__('The employee has been saved'));
				$this->redirect(array('action' => 'index'));
			   }
			   else
			   {
					$this->Session->setFlash(__('The employee could not be saved. Please, try again.'));
			   }
			}
			
		}
		 else {
			//Getting Employee Information
			$options = array('conditions' => array('Employee.' . $this->Employee->primaryKey => $id), 'recursive' => 2);
			$this->request->data = $this->Employee->find('first', $options);
			
			$this->request->data['Employee']['e_dateStart'] = date('d-m-Y', strtotime($this->request->data['Employee']['e_dateStart']));
			//debug($this->request->data);
			$employeeInfo = $this->Employee->find('first', $options);
			// Query Additional Data Required for Dropdown
			$this->loadModel('JobLvl');
			$this->loadModel('JobType');
			$this->loadModel('JobStatus');
			$jobLvls = $this->JobLvl->find('list');
			$jobStatuses = $this->JobStatus->find('list');
			$jobTypes = $this->JobType->find('list');
			$this->set(compact('jobLvls', 'jobStatuses', 'jobTypes', 'employeeInfo'));
			
		}
		
		
	}

/**
 * delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
	
	if (CakeSession::read("Auth.User.UserType") != 'Administrator') {
        // No match, redirect the user back to the index action.
        $this->Session->setFlash(__('You cant access this!'));
        $this->redirect('/');
    }
		$this->Employee->id = $id;
		if (!$this->Employee->exists()) {
			throw new NotFoundException(__('Invalid employee'));
		}
		$this->request->onlyAllow('post', 'delete');
		if ($this->Employee->delete()) {
			$this->Session->setFlash(__('Employee deleted'));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('Employee was not deleted'));
		$this->redirect(array('action' => 'index'));
	}
}
